<?php

namespace App\Orchid\Screens\Post;

use App\Models\Post;
use App\Models\Category;
use App\Orchid\Layouts\Post\PostListLayout;
use App\Orchid\Layouts\Post\PostFiltersLayout;
use Orchid\Screen\Screen;
use Orchid\Screen\Actions\Link;
use Orchid\Support\Facades\Toast;
use Orchid\Support\Facades\Layout;
use Orchid\Screen\TD;

class PostListScreen extends Screen
{
    /**
     * Fetch data to be displayed on the screen.
     *
     * @return array
     */
    public function query(): iterable
    {
        $query = Post::with(['user', 'category'])
            ->filters()
            ->defaultSort('created_at', 'desc');
            
        return [
            'posts' => $query->paginate(),
        ];
    }

    /**
     * The name of the screen displayed in the header.
     */
    public function name(): ?string
    {
        return '文章管理';
    }

    /**
     * Display header description.
     */
    public function description(): ?string
    {
        return '管理所有博客文章';
    }

    /**
     * The screen's action buttons.
     *
     * @return \Orchid\Screen\Action[]
     */
    public function commandBar(): iterable
    {
        return [
            Link::make('新建文章')
                ->icon('plus')
                ->route('platform.posts.create'),
        ];
    }

    /**
     * The screen's layout elements.
     *
     * @return \Orchid\Screen\Layout[]
     */
    public function layout(): iterable
    {
        return [
            PostFiltersLayout::class,
            PostListLayout::class,
        ];
    }
    
    public function remove(Post $post)
    {
        $post->delete();
        
        Toast::info('文章已删除');
    }
}